package com.soft.line.controller;

import com.soft.line.common.AppletSystemConstant;
import com.soft.line.entity.AppletCertificate;
import com.soft.line.service.AppletCertificateService;
import com.soft.line.util.MP.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * 小程序证书
 *
 * @author wpb
 * @version 1.0
 * @date 2020/6/3 15:13
 */
@RestController
@RequestMapping("/applet")
public class AppletCertificateController {

    @Autowired
    private AppletCertificateService appletCertificateService;

    /**
     * 小程序接口使用
     * 获取每个部门的证书总数
     *
     * @return
     */
    @PostMapping("/getCertificateNum")
    public AjaxResult getCertificateNum() {
        return appletCertificateService.getCertificateNum();
    }

    /**
     * 根据证书类型获取证书年列表数据
     *
     * @param paramMap 参数
     * @return
     */
    @PostMapping("/getCertificateYearList")
    public AjaxResult getCertificateYearList(@RequestBody Map<String, Object> paramMap) {
        if (!StringUtils.hasText(paramMap.get(AppletSystemConstant.CERT_TYPE).toString())) {
            return AjaxResult.error("证书类型不为空");
        }
        return appletCertificateService.getCertificateYearList(paramMap);
    }

    /**
     * 根据证书类型cert_type和起始日期年份以及当前页、每页显示条数分页查询证书年列表
     *
     * @param paramMap 参数
     * @return
     */
    @PostMapping("/getCertificateList")
    public AjaxResult getCertificateList(@RequestBody Map<String, Object> paramMap) {
        if (!StringUtils.hasText(paramMap.get(AppletSystemConstant.CERT_TYPE).toString())) {
            return AjaxResult.error("证书类型不为空");
        }
        if (!StringUtils.hasText(String.valueOf(paramMap.get(AppletSystemConstant.PAGE_NUM)))) {
            return AjaxResult.error("第几页不能为空");
        }
        if (!StringUtils.hasText(String.valueOf(paramMap.get(AppletSystemConstant.DATA_NUM)))) {
            return AjaxResult.error("条数不能为空");
        }
        return appletCertificateService.getCertificateList(paramMap);
    }

    /**
     * 小程序使用
     * 根据证书id查询证书信息
     *
     * @param paramMap 参数
     * @return
     */
    @PostMapping("/getCertificate")
    public AjaxResult getCertificate(@RequestBody Map<String, Object> paramMap) {
        if(paramMap.get(AppletSystemConstant.ID) == null){
            return AjaxResult.error("请选择证书");
        }
        return appletCertificateService.getCertificate(paramMap);
    }

    /**
     * 小程序使用
     * 修改证书信息
     *
     * @param appletCertificate 参数
     * @return
     */
    @PostMapping("/updateCertificate")
    public AjaxResult updateCertificate(@Validated @RequestBody AppletCertificate appletCertificate) {

        return appletCertificateService.updateCertificate(appletCertificate);
    }

    /**
     * 小程序使用
     * 删除证书
     *
     * @param paramMap
     * @return
     */
    @PostMapping("/deleteCertificate")
    public AjaxResult deleteCertificate(@RequestBody Map<String, Object> paramMap) {
        if(!StringUtils.hasText(paramMap.get(AppletSystemConstant.ID).toString())){
            return AjaxResult.error("证书唯一码不存在");
        }
        return appletCertificateService.deleteCertificate(paramMap);
    }

    /**
     * 小程序使用
     * 根据证书名称分页模糊查询证书列表
     *
     * @param paramMap
     * @return
     */
    @PostMapping("/getCertificateSearchList")
    public AjaxResult getCertificateSearchList(@RequestBody Map<String, Object> paramMap) {
        if (!StringUtils.hasText(String.valueOf(paramMap.get(AppletSystemConstant.PAGE_NUM)))) {
            return AjaxResult.error("第几页不能为空");
        }
        if (!StringUtils.hasText(String.valueOf(paramMap.get(AppletSystemConstant.DATA_NUM)))) {
            return AjaxResult.error("条数不能为空");
        }
        return appletCertificateService.getCertificateSearchList(paramMap);
    }

    /**
     * 小程序使用
     * 新增证书
     *
     * @param appletCertificate 参数
     * @return
     */
    @PostMapping("/addCertificate")
    public AjaxResult addCertificate(@Validated @RequestBody AppletCertificate appletCertificate) {
        return appletCertificateService.addCertificate(appletCertificate);
    }

    /**
     * 小程序使用
     * 根据证书详情信息分页查询证书列表
     *
     * @param paramMap 参数
     * @return
     */
    @PostMapping("/getCertificateQueryList")
    public AjaxResult getCertificateQueryList(@RequestBody Map<String, Object> paramMap) {
        return appletCertificateService.getCertificateQueryList(paramMap);
    }

}
